package atguigu.crowd.mvc.interceptor;

import atguigu.crowd.constant.CrowdConstant;
import atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.config.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 86153
 */
@Controller
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 获取session域中的admin对象
        Admin admin = (Admin)request.getSession().getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        if(admin == null){

            // 抛出异常，不在这里进行请求转发，把任务交给异常处理类来解决
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }

        // 放行
        return true;
    }
}
